<?php

namespace addons\mental\model;

use think\Model;
use addons\mental\model\Question;
use think\Db;

class Testson extends Model
{

    // 表名
    protected $name = 'mental_testson';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'userAnswer'
    ];

    public function getSkipAttr($value)
    {
        return json_decode($value, true);
    }
    public function getOptionAttr($value)
    {
        return json_decode($value, true);
    }
    public function getResultAttr($value)
    {
        return json_decode($value, true);
    }

    public function getUserAnswerAttr()
    {
        return '';
    }
    /**
     * Undocumented function
     * 统计分数
     * @param [type] $main_id
     * @param [type] $result
     * @return void
     */
    public function totalScore($main_id, $result)
    {
        $data = $this->with('Question')->field('main_id,question_id')->where('main_id', '=', $main_id)->select();
//        halt(count($result));
        if (count($result) != count($data)) {
            return -1;
        }
        $score = 0;
        foreach ($data as $val) {
            foreach ($result as $rv) {
                if ($val['question_id'] == $rv['question_id']) {
                    foreach ($val['wen_ti'] as $wv) {
                        if ($wv['option'] == $rv['userAnswer']) {
                            $score += $wv['score'];
                        }
                    }
                }
            }
        }
        return $score;
    }


    public function getDimension($main_id, $result,$dimensionArr)
    {
        
        $dimensionArr = (array)$dimensionArr;
        // var_dump($dimensionArr);die;
        foreach ($dimensionArr as $kk=>$vv){
            
            $vcc  = (array)$vv;
            $vcc['num'] = 0;
            $vcc['score'] = 0;
            
            $dimensionArr[$kk] = $vcc;
        }
        
        $data = $this->with('Question')->field('main_id,question_id')->where('main_id', '=', $main_id)->select();
        $arr = [];

        if (count($result) != count($data)) {
            return -1;
        }
        $i = 0;
        $score = 0;
        foreach ($data as $val) {
            foreach ($result as $rv) {
                if ($val['question_id'] == $rv['question_id']) {
                    foreach ($val['wen_ti'] as $wv) {
                        // var_dump($wv);die;
                        if ($wv['option'] == $rv['userAnswer']) {
                            $score += $wv['score'];
                            
                            if ($wv['score'] > 0) {
                                foreach ($dimensionArr as $kk=>$vv){
                                    if ($vv['key'] == $wv['dimension']) {
    
                                        $dimensionArr[$kk]['num'] += 1;
                                        $dimensionArr[$kk]['score'] += $wv['score'];
                                    }
                                }
                            }
                            
                            
                        }
                    }
                }
            }
        }
        return $dimensionArr;
    }

    /**
     * @Description 分量统计分数
     */
    public function liangTotal($main_id, $result, $question_ids)
    {
        $question = (new Question())->where('id', 'in', $question_ids)->select();
        $score_list = [];
        foreach ($result as $key => $item) {
            $score = 0;
            foreach ($item as $res) {
                $score += $this->scores($res, $question);
            }
            //判断求和还是求平均分
            $type = Db::name('mental_result')->where('main_id', $main_id)->where('component_id', $key)->value('type');
            if ($type == 2) {
                $pj = round($score / count($item),2);
                $score_list[$key]['score'] = $pj;
            } else {
                $score_list[$key]['score'] = $score;
            }
        }
        return $score_list;
    }


    private function scores($res, $question)
    {
        $score = 0;
        foreach ($question as $item) {
            if ($res['question_id'] == $item['id']) {
                foreach ($item['question'] as $que) {
                    if ($que['option'] == $res['userAnswer']) {
                        $score = is_numeric($que['score'])?$que['score']:0;
                        break;
                    }
                }
                break;
            }
        }
        return $score;
    }


    public function Question()
    {
        return $this->hasOne('Question', 'id', 'question_id')->bind(['type','title','image','dimensionKey', 'wen_ti' => 'question']);
    }
}
